//η φόρμα που δημιουργεί-τροποποιεί άλμπουμ καλλιτεχνών
//author: Μάνος Μαυρέλος
package album;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.persistence.Query;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import radiostation.DBManager;
import radiostation_POJOS.*;

public class NewOrModifyAlbumArtistForm2 extends javax.swing.JFrame {
    //declarations
    Long id;
    Album a;
    boolean mod;
  //apo mano: constructor
   public NewOrModifyAlbumArtistForm2(Album a,boolean mod) {
        this.a=a;
        this.mod=mod;
        initComponents();
        setLocationRelativeTo(null);//κεντράρισμα της φόρμας
        //από μανο: γεμισμα comboboxes
       //εδώ γεμίζω το disk number
       cmbDiskNum.removeAllItems();
        for (int i=1; i<10; i++){
            cmbDiskNum.addItem(i);
        }    
       //εδώ ανοίγω connection με τη βαση για να γεμισω τα combos
        DBManager.openConnection();
        Query q;
        q = DBManager.getEm().createQuery("SELECT mp FROM Musicproductioncompany mp ", Musicproductioncompany.class);
        List result = q.getResultList();
        if (!result.isEmpty()) { // βρήκα τη μέθοδο από το ίντερνετ
            Iterator itr=result.iterator();
            while (itr.hasNext()){
                Musicproductioncompany mp = (Musicproductioncompany)itr.next();
                cmbMusicCompany.addItem(mp);
            }
        }
        q = DBManager.getEm().createQuery("SELECT ar FROM Artist ar ", Artist.class);
        result = q.getResultList();
        if (!result.isEmpty()) { 
            Iterator itr=result.iterator();
            while (itr.hasNext()){
                Artist mg = (Artist)itr.next();
                cmbArtists.addItem(mg);
            }
        }
        if (a!=null){//αν μεταβάλεις το υπαρχον album
            txtTitle.setText(a.getTitle());
            cmbArtists.setSelectedItem(a.getFkArtistId());
            jDateChooser1.setDate(a.getReleasedate()); 
            cmbType.setSelectedItem(a.getType());
            cmbDiskNum.setSelectedItem(a.getDisknumber());
            cmbMusicCompany.setSelectedItem(a.getFkMusiccompanyid());
        }
         else{//αν το album είναι καινούργιο
            this.a=new Album();
            this.a.setSongList(new ArrayList()); 
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadiostationPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadiostationPU").createEntityManager();
        songQuery = DBManager.getEm().createQuery("SELECT s FROM Song s WHERE s.fkAlbumid = :fkAlbumid").setParameter("fkAlbumid", a);
        songList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(songQuery.getResultList());
        jPanel1 = new javax.swing.JPanel();
        txtTitle = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        cmbArtists = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        cmbType = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        cmbDiskNum = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cmbMusicCompany = new javax.swing.JComboBox();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jPanel2 = new javax.swing.JPanel();
        txtSongName = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtSongDur = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtSongTrackNum = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        btnAddSong = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblSongs = new javax.swing.JTable();
        jLabel10 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        btnDelSong = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        txtTitle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTitleActionPerformed(evt);
            }
        });

        jLabel1.setText("Τίτλος Άλμπουμ");

        jLabel2.setText("Καλλιτέχνης");

        jLabel5.setText("Τύπος");

        cmbType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "cd single (cs)", "extended_play (ep)", "long_play (lp)", "διπλό long_play (lp)", "τριπλό long_play (lp)", "τετραπλό long_play (lp)" }));

        jLabel6.setText("Αριθμός Δίσκων");

        cmbDiskNum.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));

        jLabel3.setText("Εταιρεία");

        jLabel4.setText("Ημερομηνία");

        jDateChooser1.setDateFormatString("yyyy-MM-dd");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addGap(38, 38, 38))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(68, 68, 68)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbArtists, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4))))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jDateChooser1, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                            .addComponent(cmbMusicCompany, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(cmbDiskNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(55, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(cmbMusicCompany, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(cmbArtists, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(5, 5, 5))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(cmbDiskNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        txtSongName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSongNameActionPerformed(evt);
            }
        });

        jLabel7.setText("Τίτλος");

        jLabel8.setText("Διάρκεια (sec)");

        jLabel9.setText("Αρ.Σειράς");

        btnAddSong.setText("Προσθήκη Τραγουδιού");
        btnAddSong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddSongActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSongName, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtSongDur, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtSongTrackNum, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnAddSong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtSongName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(txtSongDur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(txtSongTrackNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAddSong))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblSongs.setAutoCreateRowSorter(true);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, songList, tblSongs);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tracknumber}"));
        columnBinding.setColumnName("Track");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("Τίτλος");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${duration}"));
        columnBinding.setColumnName("Διάρκεια(sec)");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(tblSongs);
        if (tblSongs.getColumnModel().getColumnCount() > 0) {
            tblSongs.getColumnModel().getColumn(0).setMinWidth(5);
            tblSongs.getColumnModel().getColumn(0).setPreferredWidth(5);
            tblSongs.getColumnModel().getColumn(1).setMinWidth(20);
            tblSongs.getColumnModel().getColumn(1).setPreferredWidth(20);
            tblSongs.getColumnModel().getColumn(2).setMinWidth(5);
            tblSongs.getColumnModel().getColumn(2).setPreferredWidth(5);
        }

        jLabel10.setFont(new java.awt.Font("Monotype Corsiva", 1, 18)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(153, 0, 51));
        jLabel10.setText("Επεξεργασία Album  Κ α λ λ ι τ έ χ ν η");

        btnDelSong.setText("Διαγραφή Τραγουδιού");
        btnDelSong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelSongActionPerformed(evt);
            }
        });

        btnCancel.setText("Άκυρο");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnSave.setText("Αποθήκευση Άλμπουμ");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnDelSong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 20, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(btnSave)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancel)
                .addGap(13, 13, 13)
                .addComponent(btnDelSong)
                .addContainerGap(43, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 474, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(171, 171, 171)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(jLabel10)
                .addGap(19, 19, 19)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtTitleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTitleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTitleActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // αποθήκευση αφού γίνει έλεγχος validation
        if (!txtTitle.getText().equals("") && cmbArtists.getItemCount()>0 && !songList.isEmpty()){
        if (!DBManager.getEm().getTransaction().isActive()){
            DBManager.getEm().getTransaction().begin();
        }
        try{//αν γίνεται τροποοίηση
            if(this.mod){//για να είναι managed
            Album a2= DBManager.getEm().createNamedQuery("Album.findByAlbumId", Album.class).setParameter("albumId", this.a.getAlbumId()).getSingleResult();
            a2.setTitle(txtTitle.getText());
            a2.setFkArtistId((Artist)cmbArtists.getSelectedItem());            
            a2.setType((String)cmbType.getSelectedItem());        
            a2.setDisknumber((int)cmbDiskNum.getSelectedItem());
            a2.setFkMusiccompanyid((Musicproductioncompany)cmbMusicCompany.getSelectedItem());
            a2.setReleasedate(jDateChooser1.getDate());
            a2.setSongList(songList);
            }
            this.a.setTitle(txtTitle.getText());
            this.a.setFkArtistId((Artist)cmbArtists.getSelectedItem());            
            this.a.setType((String)cmbType.getSelectedItem());                    
            this.a.setDisknumber((int)cmbDiskNum.getSelectedItem());
            this.a.setFkMusiccompanyid((Musicproductioncompany)cmbMusicCompany.getSelectedItem());
            this.a.setReleasedate(jDateChooser1.getDate());
            this.a.setSongList(songList);
            if(!this.mod){
                DBManager.getEm().persist(this.a);//προσθήκη στη transaction με τη βάση
            }
                DBManager.getEm().getTransaction().commit();//πραγματοποίηση αλλαγών στη βάση
                this.dispose();//kleisimo formas
        } catch (Exception e) {
            e.printStackTrace();
            DBManager.getEm().getTransaction().rollback();
        }  }
         else{
                 JOptionPane.showMessageDialog(null, "Συμπληρώστε το ονομα του άλμπουμ, ένα τραγούδι και επιλέξτε καλλιτέχνη", "Σφάλμα", JOptionPane.ERROR_MESSAGE);
            }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed

        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnAddSongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddSongActionPerformed
        //validation για άκυρους χαρακτήρες στη διάρκεια
        txtSongDur.setText(txtSongDur.getText().trim());
        Pattern p = Pattern.compile(".*[a-zA-Zα-ωΑ-Ω !@#$%^&*()_+=<>?].*");
        Pattern p1 = Pattern.compile(".*[:].*");
        Matcher m3= p1.matcher(txtSongDur.getText());
        Matcher m = p.matcher(txtSongDur.getText());
        Matcher m2 = p.matcher(txtSongTrackNum.getText());
if (!m.find() && !m2.find() && !txtSongDur.getText().equals("")&& !txtSongTrackNum.getText().equals("")){
        try{
            int songDur;
            int SongTrackNum;
            String songName=txtSongName.getText();
            if(m3.find()){//μετατροπή λεπτών σε δευτερόλεπτα
              String timestampStr = txtSongDur.getText();
                String[] tokens = timestampStr.split(":");
                int minutes = Integer.parseInt(tokens[0]);
                int seconds = Integer.parseInt(tokens[1]);
                int duration =  60 * minutes + seconds;  
                songDur=duration;
            }
            else{
               songDur = Integer.parseInt(txtSongDur.getText()); 
            }
            SongTrackNum = Integer.parseInt(txtSongTrackNum.getText());
            if (songDur>0 && SongTrackNum>0 && !songName.equals("")){
                Song song=new Song();
                song.setTitle(txtSongName.getText());
                song.setDuration(songDur);
                song.setTracknumber(SongTrackNum); 
                Iterator itr=songList.iterator();//μέθοδος που βρήκα στο ίντερνετ
                boolean found=false;
                while (itr.hasNext()){
                    Song s1=(Song)itr.next();
                    if (s1.getTitle().equals(song.getTitle()) && Objects.equals(s1.getDuration(), song.getDuration()) && Objects.equals(s1.getTracknumber(), song.getTracknumber())){
                        found=true;
                        break;
                    }
                }      
                if (!found){
                    song.setFkAlbumid(a); 
                    songList.add(song);
                    a.getSongList().add(song);
                    txtSongName.setText("");
                    txtSongDur.setText("");
                    txtSongTrackNum.setText("");
                }
                else{
                    JOptionPane.showMessageDialog(null, "Το τραγούδι υπάρχει ήδη", "Σφάλμα", JOptionPane.ERROR_MESSAGE);
               }
           }
            else{
                 JOptionPane.showMessageDialog(null, "Συμπληρώστε όλα τα πεδία για το τραγούδι", "Σφάλμα", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBManager.getEm().getTransaction().rollback();
        } 
                       }
                else{
                    JOptionPane.showMessageDialog(null, "Δώστε μόνο αριθμούς χωρίς κενά", "Σφάλμα", JOptionPane.ERROR_MESSAGE);
               }
    }//GEN-LAST:event_btnAddSongActionPerformed

    private void txtSongNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSongNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtSongNameActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // όταν έχουμε έξοδο απότη φόρμα ανοίγει η προηγούμενη
        ListAlbumArtistForm ListAlbumArtistForm = new ListAlbumArtistForm(); 
        ListAlbumArtistForm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
        ListAlbumArtistForm.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_formWindowClosed

    private void btnDelSongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelSongActionPerformed
        // όταν σβήνει το τραγούδι το σβήνω κατευθείαν από τη βάση:
        int num=tblSongs.getSelectedRow();
      if (num>=0){//αν επέλεξα κάτι από τα τραγοδια
        Song s2del = songList.get(num);
        songList.remove(num);
        a.getSongList().remove(s2del);
        if (!DBManager.getEm().getTransaction().isActive()){
            DBManager.getEm().getTransaction().begin();
        }
        DBManager.getEm().remove(s2del);
        DBManager.getEm().getTransaction().commit();
        }
        else{
            JOptionPane.showMessageDialog(null, "Πρέπει να επιλέξετε τραγούδι πρώτα", "Σφάλμα", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnDelSongActionPerformed

    /**
     * @param args the command line arguments
     */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager RadiostationPUEntityManager;
    private javax.swing.JButton btnAddSong;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDelSong;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cmbArtists;
    private javax.swing.JComboBox cmbDiskNum;
    private javax.swing.JComboBox cmbMusicCompany;
    private javax.swing.JComboBox cmbType;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private java.util.List<radiostation_POJOS.Song> songList;
    private javax.persistence.Query songQuery;
    private javax.swing.JTable tblSongs;
    private javax.swing.JTextField txtSongDur;
    private javax.swing.JTextField txtSongName;
    private javax.swing.JTextField txtSongTrackNum;
    private javax.swing.JTextField txtTitle;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
